package com.ims.service.impl;



import com.b2c.common.third.thymeleaf.PagingResponse;
import com.b2c.common.utils.MD5Utils;
import com.b2c.common.utils.RandomUtils;
import com.b2c.common.utils.ResultVo;
import com.b2c.common.utils.ResultVoEnum;
import com.ims.entity.ManageUserEntity;
import com.ims.entity.vo.UserMenuVo;
import com.ims.repository.ManageUserRepository;
import com.ims.service.ManageUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 描述：
 * 管理员用户Service
 *
 * @author qlp
 * @date 2018-12-26 5:22 PM
 */
@Service
public class ManageUserServiceImpl implements ManageUserService {
    @Autowired
    private ManageUserRepository userRepository;

    /**
     * 管理员登录
     *
     * @param userName
     * @param userPwd
     * @return
     */
    @Override
    public ResultVo<ManageUserEntity> userLogin(String userName, String userPwd) {
        ManageUserEntity entity = userRepository.getUserByUserName(userName);
        if (entity == null) return new ResultVo(ResultVoEnum.NotFound, "用户不存在");
        String pwd = MD5Utils.MD5Encode(userPwd);
        String finalPwd = MD5Utils.MD5Encode(pwd + entity.getUserSalf());
        if (finalPwd.equals(entity.getUserPwd()) == false) return new ResultVo(ResultVoEnum.DataError, "登录密码不正确");


        return new ResultVo<ManageUserEntity>(ResultVoEnum.SUCCESS, entity);
    }

    @Override
    public String getNameById(Integer id) {
        return userRepository.getNameById(id);
    }

    @Override
    public ResultVo<ManageUserEntity> updManageUserPwd(int userId, String oldPwd, String newPwd) {
        ManageUserEntity user = userRepository.getManageUserById(userId);
        if(user==null)return new ResultVo(ResultVoEnum.DataError, "用户不存在");
        String finalPwd = MD5Utils.MD5Encode(MD5Utils.MD5Encode(oldPwd) + user.getUserSalf());
        if (finalPwd.equals(user.getUserPwd()) == false) return new ResultVo(ResultVoEnum.DataError, "旧密码不正确");
        String pwd_rand = RandomUtils.randomString(8);
        userRepository.updManageUserPwd(userId, MD5Utils.MD5Encode(MD5Utils.MD5Encode(newPwd) + pwd_rand), pwd_rand);
        return new ResultVo<>(ResultVoEnum.SUCCESS, user);
    }

    @Override
    public PagingResponse<ManageUserEntity> getUserList(Integer pageIndex, Integer pageSize, String name, String mobile) {
        return userRepository.getUserList(pageIndex,pageSize,name,mobile);
    }

    @Override
    public Integer delUser(Integer id) {
        return userRepository.delUser(id);
    }

    @Override
    public ResultVo<Integer> updAddUser(Integer userId, String name, String mobile, String user, String pwd, String state,String groupName) {
        int mobileCount = userRepository.checkManageUserByMobile(userId, mobile);

        if (mobileCount > 0) return new ResultVo<>(ResultVoEnum.Exist, "手机号已存在");

        if (userId==0){
            Integer userName = userRepository.checkUserName(user);
            if (userName!=0) return new ResultVo<>(ResultVoEnum.Exist, "用户已存在");
        }

        userRepository.updAddUser(userId, name, mobile,user, pwd, state,groupName);
        return new ResultVo<>(ResultVoEnum.SUCCESS);
    }

    @Override
    public boolean checkUserAuth(Integer userId, String requestUrl) {
        return userRepository.checkUserAuth(userId,requestUrl);
    }

    @Override
    public void initUserPermissionMenu(Integer userId) {
        userRepository.initUserPermissionMenu(userId);
    }

    @Override
    public List<UserMenuVo> getUserPerMissionMenu(Integer userId) {
        return userRepository.getUserPerMissionMenu(userId);
    }

    @Override
    public void setUserMenu(Integer userId, String[] menuIds) {
        userRepository.setUserMenu(userId,menuIds);
    }
}
